package com.collaboration.electron.controllers;


import com.collaboration.electron.dao.UserRepository;
import com.collaboration.electron.entities.U;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author pinzi
 * @param
 * @return 无
 * 这个类是用户表的控制层
 */

@Controller
@RequestMapping("")
public class SecurityController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping("register")
    public String displayRegister(Model model){
        U user = new U();
        model.addAttribute( "U", user);
        return "register";
    }


    @PostMapping("register/save")
    public String saveUser(Model model, U user){
        user.setUserPassword(bCryptPasswordEncoder.encode(user.getUserPassword()));
        user.setRole("ROLE_USER");
        userRepository.save(user);
        return  "redirect:/login";
    }


    @GetMapping("")
    public String goHome(){
        return "redirect:/home";
    }

}
